package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.order.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-17
 */
@CrossOrigin
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {



    @Autowired
    private UcenterMemberService ucenterMemberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //返回token值，使用jwt生成
        String token = ucenterMemberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("regist")
    public R registUser(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.regist(registerVo);
        return R.ok();
    }


    //根据Token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类方法，根据request对象获取头信息，返回用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库获取用户信息
        UcenterMember member = ucenterMemberService.getById(id);
        return R.ok().data("memberInfo",member);
    }

    //根据用户id获取用户信息
    @PostMapping("getUserinfoOrder/{id}")
    public UcenterMemberOrder getUserinfoOrder(@PathVariable String id){

        UcenterMember member=ucenterMemberService.getById(id);

        UcenterMemberOrder memberOrder=new UcenterMemberOrder();
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }
    //查询某一天注册人数
    @GetMapping("countRegisterDay/{day}")
    public R countRegisterDay(@PathVariable String day){

        Integer count=ucenterMemberService.countRegisterDay(day);
        return R.ok().data("countRegisterDay",count);
    }

}

